/*
 * @Descripttion:
 * @Author: zhangchong zc16607@gmail.com
 * @Date: 2022-12-30 09:49:43
 * @LastEditors: zhangchong zc16607@gmail.com
 * @LastEditTime: 2023-05-29 15:42:42
 */

import { contextBridge, ipcRenderer } from 'electron'

// Expose protected methods that allow the renderer process to use the ipcRenderer without exposing the entire object. Reference: https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration
contextBridge.exposeInMainWorld('ipcRenderer', {
  receive: (channel, func) => {
    if (
      ['fromMain', 'tabs-update'].includes(
        channel
      )
    ) {
      ipcRenderer.on(channel, (event, ...args) => func(...args)) // Deliberately strip the event as it includes the sender.
    } // end if
  },
  send: (channel, data) => {
    if (
      [
        'close-tab',
        'toMain',
        'new-tab',
        'switch-tab',
        'control-ready'
      ].includes(channel)
    ) {
      ipcRenderer.send(channel, data)
    } // end if
  }
})


let invokeObj = {}
const apiInvokeKey = [
  'getCurrentDir',
  'build',
  'stopBuild',
  'queryTask'
]
apiInvokeKey.map(key => {
  invokeObj[key] = (...args) => ipcRenderer.invoke(`api:${key}`, args)
})

contextBridge.exposeInMainWorld('$api', invokeObj)



const listenObj = {}
const cancelListenObj = {}
const apiSendKey = [
  'buildInfo',
  'percent'
]
apiSendKey.map(key => {
  listenObj[key] = (callback) => ipcRenderer.on(`listen:${key}`, callback)
})
apiSendKey.map(key => {
  // cancelListenObj[key] = (callback) => ipcRenderer.removeListener(`listen:${key}`, callback)
  cancelListenObj[key] = () => ipcRenderer.removeAllListeners(`listen:${key}`)
})
contextBridge.exposeInMainWorld('$listen', listenObj)
contextBridge.exposeInMainWorld('$cancelListen', cancelListenObj)